﻿using MLSMooc.DAO.Interface.Exam;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using System.Data.SqlClient;
using MLSMooc.Helper;
using MLSMooc.BO;

namespace MLSMooc.DAO.Implement.Exam
{
   public class ExamDAO :IExamDAO
    {
        private static ExamDAO instance = new ExamDAO();
        private ExamDAO() { }
        public static ExamDAO Getinstance() => instance;

        private static readonly string queryFiled = " select  top 1 * from Mooc_ExamNew where isDelete=0";


        //获取课程下测试题列表
        public async Task<IEnumerable<ExamBO>> GetCourse_ExamList(int Cid)
        {
            using (SqlConnection conn = new SqlConnection(SalesSysSqlHelper.connectionString))
            {
                StringBuilder sql = new StringBuilder(queryFiled);
                sql.Append($@" and CourseId=@Cid and TopicType=1 order by TopicType");
                var res = await conn.QueryAsync<ExamBO>(sql.ToString(), new { Cid = Cid });
                return res;
            }

        }

        //获取正确答案
        public async Task<ExamBO> GetExamScore(int Cid, int Eid)
        {
            using (SqlConnection conn = new SqlConnection(SalesSysSqlHelper.connectionString))
            {
                StringBuilder sql = new StringBuilder($@"select RightAnswerText from Mooc_Exam where isDelete=0 and Id=@Eid and CourseId=@Cid");
                var res = await conn.QueryAsync<ExamBO>(sql.ToString(), new { Eid = Eid, Cid = Cid });
                return res.FirstOrDefault();
            }
        }
        //新建表弹框选择题获取正确答案
        public async Task<ExamBO> GetTankExamScore(int Cid, int Eid)
        {
            using (SqlConnection conn = new SqlConnection(SalesSysSqlHelper.connectionString))
            {
                StringBuilder sql = new StringBuilder($@"select RightAnswerText from Mooc_ExamNew where isDelete=0 and Id=@Eid and CourseId=@Cid");
                var res = await conn.QueryAsync<ExamBO>(sql.ToString(), new { Eid = Eid, Cid = Cid });
                return res.FirstOrDefault();
            }
        }

        //获取用户答题次数
        public async Task<Exam_AnswerBO> GetExamMaxAnswerNumber(int AdminId)
        {
            using (SqlConnection conn = new SqlConnection(SalesSysSqlHelper.connectionString))
            {
                StringBuilder sql = new StringBuilder($@"  select Max(AnswerNumber) AnswerNumber from Mooc_Exam_Answer where isDelete=0 and AdminId=@AdminId ");
                var res = await conn.QueryAsync<Exam_AnswerBO>(sql.ToString(), new { AdminId=AdminId });
                return res.FirstOrDefault();
            }
        }
 
    }
}
